package com.qujia.web.controller.task;


import com.qujia.common.core.domain.AjaxResult;
import com.qujia.station.controller.ClassSettlementController;
import com.qujia.station.controller.StationController;
import com.qujia.station.domain.ClassSettlement;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.dto.AutoPayWayInfo;
import com.qujia.station.domain.dto.RateSettingDto;
import com.qujia.station.domain.vo.AutomaticClassInfoVo;
import com.qujia.station.mapper.AppUserInfoMapper;
import com.qujia.station.mapper.AutomaticClassMapper;
import com.qujia.station.mapper.PayWayMapper;
import com.qujia.station.service.*;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Configuration
@EnableScheduling
public class AutomaticClassTask {
    private static final Logger log = LoggerFactory.getLogger(AutomaticClassTask.class);

    @Autowired
    protected AutomaticClassMapper automaticClassMapper;

    @Autowired
    protected PayWayMapper payWayMapper;


    @Autowired
    private AutomaticClassService automaticClassService;

    @Autowired
    private ClassSettlementController classSettlementController;

    @Autowired
    private IClassSettlementService classSettlementService;

    @Autowired
    private IPayWayService payWayService;

    @Autowired
    private IStationService stationService;

    @Autowired
    private AppUserInfoMapper appUserInfoMapper;


    @Autowired
    private IPayOrderAllService payOrderAllService;

    //  @Scheduled(cron = "0 0/2 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")
    private void AutomaticTask() throws ParseException, InterruptedException {
        List<AutomaticClassInfoVo> automaticClasses = automaticClassMapper.selectAutomaticClassBystatus(0);
        log.info("自动班结开始");
        if (ObjectUtils.isNotEmpty(automaticClasses)) {
            for (AutomaticClassInfoVo automaticClassInfoVo : automaticClasses) {
                try {

                    Calendar calendar = Calendar.getInstance();
                    //获取数据库时间
                    String knotTime = automaticClassInfoVo.getKnotTime();
                    String[] split = knotTime.split(":");
                    String hour = split[0];
                    //获取当前时间
                    calendar.setTime(new Date());
                    int currentHour = calendar.get(Calendar.HOUR_OF_DAY);//时（24小时制）
                    String currentHour1 = "";
                    currentHour1 = String.valueOf(currentHour);
                    if (currentHour < 10) {
                        currentHour1 = "0" + currentHour;
                    }


                    Date currentDate = new Date();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                    String currentDateString = dateFormat.format(currentDate); // 当前日期的年月日字符串


                    String completeDateString = currentDateString + " " + hour + ":" + "00" + ":" + "00";
                    SimpleDateFormat completeDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date completeDate = completeDateFormat.parse(completeDateString);


                    if (hour.equals(currentHour1)) {
                        log.info("自动班结 ————————————————油站id：{}，班结时间：{},用户id：{}", automaticClassInfoVo.getStationId(), automaticClassInfoVo.getKnotTime(), automaticClassInfoVo.getUserId());
                        ClassSettlement classSettlement = new ClassSettlement();
                        classSettlement.setStationId(automaticClassInfoVo.getStationId());
                        classSettlement.setClassEndTime(completeDate);
//                classSettlement.setClassStartTime(automaticClassInfoVo.getKnotTime());
                        classSettlement.setCreateUser(automaticClassInfoVo.getUserId());
                        classSettlement.setCreateName(automaticClassInfoVo.getUserName());
//                    automaticClassService.createClassSe(classSettlement);
                        classSettlementController.createClassSe(classSettlement);
                    }
                } catch (Exception e) {

                    e.printStackTrace();
                }

            }
        }
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    private void AutoPayWayTask() throws ParseException, InterruptedException {
//        List<AutoPayWayInfo> autoPayWayInfos =  payWayMapper.selectAutoPayWay();
        payWayService.selectAutoPayWay(0, 0L, "0");
    }

    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨 0 点执行定时任务
    public void payOrderAll() {
        payOrderAllService.addNewPayOrder();
    }


    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨 0 点执行定时任务
    public void setUsPayWay() {
        payWayService.selectMnoAllRateList();

    }

    @Scheduled(cron = "0 0 8 * * ?")//确认日经营报表每天 8 点执行定时任务
    public void createClassReportOnEight() {
        Station checkStation = new Station();
        checkStation.setStatus("1");
        checkStation.setDeleteStatus("0");
        List<Station> stationList = stationService.selectStationList(checkStation);
        for (Station station : stationList) {
            try {
                classSettlementService.insertBusinessDailyReport(station.getStationId(), 2, "1", null, null);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

    }

    @Scheduled(cron = "0 0 0 * * ?")//确认日经营报表每天凌晨 0 点执行定时任务
    public void createClassReport() {
        Station checkStation = new Station();
        checkStation.setStatus("1");
        checkStation.setDeleteStatus("0");
        List<Station> stationList = stationService.selectStationList(checkStation);
        for (Station station : stationList) {
            try {
                classSettlementService.insertBusinessDailyReport(station.getStationId(), 0, "1", null, null);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

    }

    @Scheduled(cron = "0 0 8 1 * ?")//月账单提醒
    public void getTotalAmountByPayDate() {
        LocalDateTime startTime = LocalDateTime.now().minusMonths(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = LocalDateTime.now().withDayOfMonth(1).minusDays(1).withHour(23).withMinute(59).withSecond(59);

        List<String> appUserMobilePhone = appUserInfoMapper.getAppUserMobilePhone(null);
        payOrderAllService.getTotalAmountByPayDate(startTime,endTime,appUserMobilePhone);
    }
}
